package com.OrderingServer.service.impl;

import com.OrderingServer.mapper.ProductCategoryMapper;
import com.OrderingServer.pojo.ProductCategory;
import com.OrderingServer.pojo.ProductInfo;
import com.OrderingServer.mapper.ProductInfoMapper;
import com.OrderingServer.service.ProductInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xingo
 * @since 2025-08-11
 */
@Service
public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoMapper, ProductInfo> implements ProductInfoService {
    private final ProductInfoMapper productInfoMapper;
    private final ProductCategoryMapper productCategoryMapper;

    public ProductInfoServiceImpl(ProductInfoMapper productInfoMapper, ProductCategoryMapper productCategoryMapper) {
        this.productInfoMapper = productInfoMapper;
        this.productCategoryMapper = productCategoryMapper;
    }

    //分页
    @Override
    public PageInfo<ProductInfo> selectPage(Integer pageNum, Integer pageSize, ProductInfo productInfo) {
        //设置分页插件
        PageHelper.startPage(pageNum, pageSize);
        //正常查询，查询数据
        QueryWrapper<ProductInfo> queryWrapper = new QueryWrapper<ProductInfo>();
        queryWrapper.like(true,"product_name",productInfo.getProductName());
        queryWrapper.eq("del","0");//未删除
        List<ProductInfo> list = productInfoMapper.selectList(queryWrapper);
        for (ProductInfo info : list) {
            ProductCategory productCategory = productCategoryMapper.selectById(info.getCategoryType());
            info.setCategoryName(productCategory.getCategoryName());
        }
        
        return PageInfo.of(list);
    }

}
